
package com.example.villagemanager.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.example.villagemanager.R;
import com.example.villagemanager.VillageManagerApp;
import com.example.villagemanager.config.Constant;
import com.example.villagemanager.model.entity.BaseResponse;
import com.example.villagemanager.model.event.Event;
import com.example.villagemanager.model.response.UserLoginResp;
import com.example.villagemanager.network.BaseObserver;
import com.example.villagemanager.network.RetrofitHelper;
import com.example.villagemanager.network.RxUtil;
import com.example.villagemanager.network.api.AppInfoApi;
import com.example.villagemanager.ui.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_account)
    EditText etAccount;

    @BindView(R.id.et_password)
    EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initActivityData() {

    }

    @Override
    protected void initContentView() {

    }

    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }

    @OnClick({R.id.btn_login})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_login:
                userLogin();
                break;
        }
    }

    private void userLogin() {
        String account=etAccount.getText().toString().trim();
        String password=etPassword.getText().toString().trim();
        RetrofitHelper.create(AppInfoApi.class)
                .userLogin(account,password,1)
                .compose(RxUtil.observervableToMain())
                .as(RxUtil.bindLifecycle( this))
                .subscribe(new BaseObserver<UserLoginResp>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<UserLoginResp> response) {
                        if (response.getData()!=null) {
                            VillageManagerApp.userLoginResp=response.getData();
                            EventBus.getDefault().post(new Event<>(Constant.LOGIN_SUCCESS_ACTION));




                        }
                    }
                });

    }

    @Override
    protected void onEventProcess(Event event) {
        int code=event.getCode();
        switch (code){
            case Constant.LOGIN_SUCCESS_ACTION:

                sendIntent(MainActivity.class);
                finish();
                break;
        }
    }
}